<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Config;
use think\Session;
use think\View;
use app\admin\model\BaseModel;
use app\admin\model\IndexModel;
use app\index\util\Jpush;

class Base extends Controller
{

    public $setting = [];
    public $uploadType;
    public function __construct(Request $request)
    {
        parent::__construct();
        $view = new view();
        //Session::set('user',123);
        // $controllerName = $this->request->controller();
        // $actionName = $this->request->action();
        // echo $controllerName.'='. $actionName;die;
        $this->setting = $this->baseSetting();
        $this->uploadType = Config::get('upload_img');
        $user = $request->session('user');
        if (!$user) {
            header("location:".url('Login/login'));
            exit();
        }
    }

    //删除
    public function del()
    {
        $this->BaseModel = new BaseModel();
        $id = $this->request->post('id');
        $table = $this->request->post('table');
        $del = $this->BaseModel->del($id, $table);
        if ($del ==false) {
            echo false;
        } else {
            echo true;
        }
    }

    //批量删除
    public function dels()
    {
        $this->BaseModel = new BaseModel();
        $id = $this->request->post('id');
        $table = $this->request->post('table');
        $del = $this->BaseModel->dels($id, $table);
        if ($del == false) {
            echo false;
        } else {
            echo true;
        }
    }

   /**
     *  向用户推送消息
     *  @param string $title  标题
     *  @param string $content  内容
     *  @param array $extras  自定义内容
     *  @return bool
     */
    public function push($content, $receiver = null, $extras = null)
    {
        $title = "消息推送";
        if (empty($receiver)) {
            $receiver = 'all';
        }
        $jpush = new Jpush();
        $msg = $jpush->jiguang_send($receiver, $title, $content, $extras);
        
        if ($msg === false) {
            return false;
        }
        $msg = json_decode($msg, true);
        if (isset($msg['error'])) {
            return false;
        }
        return true;
    }
    
    //三级联动
    public function citys()
    {
        $this->BaseModel = new BaseModel();
        $provice = $this->request->post('provice');
        $list = $this->BaseModel->citys($provice);
        echo json_encode($list);
    }
    public function towns()
    {
        $this->BaseModel = new BaseModel();
        $city = $this->request->post('city');
        $list = $this->BaseModel->citys($city);
        echo json_encode($list);
    }

    //配置信息
    public function baseSetting()
    {
        $indexModel = new IndexModel();
        $data = $indexModel->getSetting();
        return array_column($data, 'para_value', 'para_name');
    }

    /**
     *  根据code获取省市区
     *  @param string $code
     *  @return string
     */
    public function city($code, $type = 0)
    {
        // $this->count++;
        $indexModel = new IndexModel();
        $city = $indexModel->getCity($code);
        if (!isset($city)) {
            return '';
        }
        $name = $city['name'];
        if ($city['pcode'] != 0) {
            // if ($this->count >= 5) {
            //     return $name;
            // }
            $this->count ++;
            switch ($type) {
                case 1:
                    $name = $this->city($city['pcode'], $type) . $name;
                    break;
                case 2:
                    $name = $this->city($city['pcode'], $type) . '/' . $name;
                    break;
                default:
                    $name = $this->city($city['pcode'], $type) . ' ' . $name;
                    break;
            }
        }
        return $name;
    }
}
